package com.active.web;

import com.active.dao.entity.ActIdUser;
import com.active.dao.mapper.ActIdUserMapper;
import com.active.service.AjaxPageResult;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.*;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/6/18 0018.
 */
@RestController
@Slf4j
@RequestMapping("leaveSign")
public class LeaveSignController {

    @Resource(name = "taskService")
    private TaskService taskService;

    @Resource(name = "runtimeService")
    private RuntimeService runtimeService;

    @Resource(name = "repositoryService")
    private RepositoryService repositoryService;

    @Resource(name = "identityService")
    private IdentityService identityService;

    @Resource(name = "historyService")
    private HistoryService historyService;

    @Resource(name = "formService")
    private FormService formService;

    @Autowired
    private ActIdUserMapper actIdUserMapper;


    @RequestMapping("sign")
    //主管分配人数
    public AjaxPageResult sign(){
        log.info("进行请假测试");

        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("leaveSign").singleResult();

        Map<String,Object> map = new HashMap<>();

        List<ActIdUser> list = actIdUserMapper.list();

        if(list == null || list.size() == 0){
            return AjaxPageResult.fail("未查到可使用的人，你都没人了");
        }

        List<String> list1 = new ArrayList<>();
        for(ActIdUser user: list){
            list1.add(user.getId());
        }

        map.put("users", list1);
        map.put("rate",0.6d);

        //运行实例
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinition.getKey(),map);

        ActIdUser user = (ActIdUser) runtimeService.getVariable(processInstance.getId(),"user");

        //找到该实例的所有人的任务
        List<Task> tasks = taskService.createTaskQuery().processInstanceId(processInstance.getId()).list();

        return AjaxPageResult.success("任务有："+tasks);

    }

    @RequestMapping("examine")
    public AjaxPageResult examine(String userId,String value){
        log.info("本次审核人：{}",userId);

        ActIdUser user = actIdUserMapper.selectByPrimaryKey(userId);

        log.info("查询当前审核人：{}",user);

        if(null == user){
            AjaxPageResult.fail("未找到该审核人");
        }

//        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().
//                processDefinitionKey("leaveSign").singleResult();
        //查询当前审核人的审核任务
        Task task = taskService.createTaskQuery().taskAssignee(userId).singleResult();

        Map<String,String> map = new HashMap<>();
        map.put("orPass",value);

        formService.submitTaskFormData(task.getId(),map);

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leaveSign");

        boolean result = (boolean)runtimeService.getVariable(processInstance.getId(),"passResult");

        return AjaxPageResult.success("审核完成:"+result);

    }
}
